package com.gh.ssyx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gh.ssyx.mapper.PermissionMapper;
import com.gh.ssyx.model.acl.Permission;
import com.gh.ssyx.model.acl.RolePermission;
import com.gh.ssyx.service.PermissionService;
import com.gh.ssyx.service.RolePermissionService;
import com.gh.ssyx.utils.PermissionHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Eric Fang
 * @date 2023/6/19 21:11
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private RolePermissionService rolePermissionService;

    @Override
    public List<Permission> queryAllPermission() {
        List<Permission> permissions = baseMapper.selectList(null);
        List<Permission> permissionList = PermissionHelper.buildPermission(permissions);
        return permissionList;
    }

    @Override
    public void removeChildById(Long id) {
        //封装所有要删除的菜单id
        List<Long> idList = new ArrayList<>();
        idList.add(id);
        List<Long> allPermissionList = this.getAllPermissionList(id, idList);
        baseMapper.deleteBatchIds(allPermissionList);
    }

    @Override
    public List<Permission> toAssignPermission(Long id) {
        //先查到所有的权限集合，再查到当前角色已分配的权限集合。遍历你的所有权限的集合，给当前角色已分配的权限的select的属性赋值true，再封装成树形结构返回就好了。
        List<Permission> allPermissions = baseMapper.selectList(null);
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId,id);
        List<RolePermission> rolePermissions = rolePermissionService.list(queryWrapper);
        for (Permission permission : allPermissions) {
            for (RolePermission rolePermission : rolePermissions) {
                if (permission.getId().equals(rolePermission.getPermissionId())) {
                    permission.setSelect(true);
                    break;
                }
            }
        }
        List<Permission> buildPermission = PermissionHelper.buildPermission(allPermissions);
        return buildPermission;
    }

    @Override
    public void saveRolePermission(Long roleId, Long[] permissionIds) {
        //删除角色已经分配过的权限
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId,roleId);
        rolePermissionService.remove(queryWrapper);
        //重新添加新的权限
        List<RolePermission> list = new ArrayList<>();
        for (Long permissionId : permissionIds) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setPermissionId(permissionId);
            rolePermission.setRoleId(roleId);
            list.add(rolePermission);
        }
        rolePermissionService.saveBatch(list);
    }

    private List<Long> getAllPermissionList(Long id, List<Long> idList) {
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Permission::getPid, id);
        List<Long> ids = baseMapper.selectList(queryWrapper)
                .stream()
                .map(Permission::getId)
                .collect(Collectors.toList());
        if (ids.size() > 0) {
            for (Long i : ids) {
                idList.add(i);
                this.getAllPermissionList(i, idList);
            }
        }
        return idList;
    }
}
